package com.forum.Service.Impl;

import com.common.Utils.JwtUtil;
import com.forum.Entity.Comment;
import com.forum.Entity.Post;
import com.forum.Entity.Stats;
import com.forum.Mapper.CommentMapper;
import com.forum.Mapper.PostMapper;
import com.forum.Service.CommentService;
import com.forum.Service.PostService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static org.bouncycastle.asn1.x500.style.RFC4519Style.title;

@Service
public class PostServiceImpl implements PostService {

    @Autowired
    private PostMapper postMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Override
    public void addPost(Post post) {
        post.setPostId(makeId());
        post.setViews(0);
        post.setLikes(0);
        post.setComments(0);

        postMapper.addPost(post);
    }

    @Override
    public PageInfo<Post> getPostList(Integer pageNum, Integer pageSize, String topicId, String title,String sortBy) {
        PageHelper.startPage(pageNum,pageSize);
        List<Post> postList;
        //排序 最新发布、最多浏览、最多评论、最多点赞
        switch (sortBy) {
            case "newest":
                postList = postMapper.getPostListByNewest(topicId, title);
                break;
            case "most_viewed":
                postList = postMapper.getPostListByMostViews(topicId, title);
                break;
            case "most_commented":
                postList = postMapper.getPostListByMostComments(topicId, title);
                break;
            case "most_liked":
                postList = postMapper.getPostListByMostLikes(topicId, title);
                break;
            default:
                postList = postMapper.getPostList(topicId, title);
                break;
        }
        return new PageInfo<>(postList);
    }
    @Override
    public PageInfo<Post> getPostLists(Integer pageNum, Integer pageSize, String topicId, String title) {
        PageHelper.startPage(pageNum,pageSize);
        List<Post> postList= postMapper.getPostLists(topicId, title);
        return new PageInfo<>(postList);
    }

    @Override
    public PageInfo<Post> getPostListByTitle(Integer pageNum, Integer pageSize, String title) {
        PageHelper.startPage(pageNum,pageSize);
        List<Post> postList= postMapper.getPostListByTitle(title);
        return new PageInfo<>(postList);
    }

    @Override
    public Post getPostDetail(String postId) {
        String userId=postMapper.getUserIdBypostId(postId);
        return postMapper.getPostDetail(postId, userId);
    }

    //更新浏览数
    @Override
    public void addViews(String postId) {
        Post post = postMapper.getPostById(postId);
        post.setViews(post.getViews()+1);
        postMapper.updatePost(post);
    }

    //更新评论数
    @Override
    public void addComments(String postId) {
        Post post = postMapper.getPostById(postId);
        post.setComments(post.getComments()+1);
        postMapper.updatePost(post);
    }

    //更新点赞数
    @Override
    public void addLikes(String targetId) {
        Post post = postMapper.getPostById(targetId);
        post.setLikes(post.getLikes()+1);
        postMapper.updatePost(post);
    }

    //取消点赞数 减1
    @Override
    public void cutLikes(String targetId) {
        Post post = postMapper.getPostById(targetId);
        post.setLikes(post.getLikes()-1);
        postMapper.updatePost(post);
    }

    @Override
    public List<Post> getHotPosts() {
        return postMapper.getHotPosts();
    }

    @Override
    public Stats getForumStatistics() {
        Stats stats=new Stats();
        stats.setPostCount(postMapper.getPostCount());
        stats.setCommentCount(commentMapper.getCommentCount());
        stats.setTodayPosts(postMapper.getTodayPosts());
        stats.setTodayComments(commentMapper.getTodayComments());
        return stats;
    }

    @Override
    public PageInfo<Post> getmyPosts(Integer pageNum, Integer pageSize, String title,String topicId,String userId, String sortBy) {
        PageHelper.startPage(pageNum,pageSize);
        List<Post> postList;
        //排序 最新发布、最多浏览、最多评论、最多点赞
        switch (sortBy) {
            case "newest":
                postList = postMapper.getMyPostListByNewest(title, topicId, userId);
                break;
            case "most_viewed":
                postList = postMapper.getMyPostListByMostViews(title, topicId, userId);
                break;
            case "most_commented":
                postList = postMapper.getMyPostListByMostComments(title, topicId, userId);
                break;
            case "most_liked":
                postList = postMapper.getMyPostListByMostLikes(title, topicId, userId);
                break;
            default:
                postList = postMapper.getMyPostList(title, topicId, userId);
                break;
        }
        return new PageInfo<>(postList);
    }

    @Override
    public void deletePost(String postId) {
        postMapper.deletePost(postId);
    }



    public String getUserIdBypostId(String postId) {
        return postMapper.getUserIdBypostId(postId);
    }

    private String makeId() {
        return "p"+ System.currentTimeMillis();
    }



}
